<template>
  <div class="report-form-content">
    <el-form class="my-form"
             ref="objForm"
             :model="obj"
             :rules="rules"
             label-position="right"
             label-width="auto"
             label-suffix="：">
      <div class="tabs-form-head">
        <span>合同请款</span>
        <div class="">
          <el-button type="primary" plain @click="returnBtn">返回</el-button>
        </div>
      </div>
      <el-divider border-style="dashed"/>
      <!--收款列表-->
      <el-row :gutter="20">
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
            <el-table :data="list"
                      style="width: 100%; margin-top: 20px"
                      row-key="id"
                      border
                      default-expand-all>
              <el-table-column align="center" label="请款金额" prop="cashAmount">
                <template v-slot="{row}"><MoneyTag :money="row.cashAmount" /></template>
              </el-table-column>
              <el-table-column align="center" label="请款状态" prop="state">
                <template v-slot="{row}">
                  <EnumTag :enum-list="getEnumList('ContractInitialCashState')" :enum-key="row.state" />
                </template>
              </el-table-column>
              <el-table-column align="center" label="请款类型" prop="cashType">
                <template v-slot="{row}">
                  <DictTag :dict-list="getDictList('ContractInitialCashType')" :dict-key="row.cashType" />
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="160px" fixed="right">
                <template v-slot="{row}">
                  <el-button type="primary" link v-if="['0','3','4'].includes(row.state)" @click="handleSubmit(row)">
                    {{row.cashType === '1' ? '申请收款' : '申请付款'}}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
      </el-row>
    </el-form>
    <selectApprove v-model:itemsVisible="dialogApproveVisible" @close="dialogApproveVisible = false" :business="'3005'" :businessId="obj.id" @doSubmit="doSubmit" />
  </div>
</template>
<script setup>
import {getCurrentInstance, ref} from 'vue'
import {doGet, doPost} from "@/config/http";
import {svc, getListPageSize, notifySuccess, alertErrorMsg, getDictList, getEnumList} from "@/config/common";
import {useRoute, useRouter} from 'vue-router'
import {getUserId} from "@/config/cookie";
import useUserStore from "@/store/modules/user";
import selectApprove from "@/views/components/base/selectApprove.vue";

const router = useRouter()
const route = useRoute()

const {proxy: ctx} = getCurrentInstance();

let objForm = ref(null)
let dialogApproveVisible = ref(false)

let list = ref([])
let catalog = ref('')
catalog.value = route.query.catalog

let obj = ref( {
  contractInitialId: route.query.id, // 合同ID
  batch: null, // 批次号
  cashType: null, // 请款类型
  cashUserId: null, // 请款经办人
  cashDepartmentId: null, // 请款经办人部门
  cashAmount: null, // 请款金额
  state: null, // 请款状态
  id: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  deleted: null,
  version: null,
})

let selectedItem = ref(null)

const rules = {}

const initObj = async () => {
  if (route.query.id) {
    let listQuery = {
      contractInitialId: route.query.id,
      batch: 'latest',
      pageNum: 1,
      pageSize: 1000,
    }
    let res = await doGet(svc, '/contract-initial-cash/page', listQuery, true)
    list.value = res['records']
  }
}

initObj()

const handleSubmit = async (row) => {
  selectedItem.value = row
  selectedItem.value.cashUserId = getUserId();
  selectedItem.value.cashDepartmentId = useUserStore().organization.id;
  dialogApproveVisible.value = true
}

const doSubmit = async (data) => {
  let v = await objForm.value.validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  selectedItem.value.cashUserId = getUserId()
  selectedItem.value.cashDepartmentId = useUserStore().organization.id;
  let o = {}
  o.oper = 'update'
  o.approveId = data.approveId
  o.approveNodes = data.approveNodes
  o.items = [selectedItem.value]
  doPost(svc, `/customContractInitial/cashContractInitial/${route.query.id}`, o, true).then(() => {
    notifySuccess('提交成功')
    returnBtn()
  })
}

const returnBtn = () => {
  switch (route.query.kind) {
    case '1':
      ctx.goTargetByName('contractInitial', {});
      break;
    case '2':
      ctx.goTargetByName('contractMain', {})
      break;
  }
}
</script>

<style scoped>
.report-form-content {
  padding: 20px;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.report-list span:nth-child(1) {
  display: inline-block;
  width: 120px;
  text-align: right;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
</style>
